var mysql = require('mysql');
const { dbConfig } = require('../config.js');
var pool = mysql.createPool(dbConfig);

var db = {};

db.query = function (sql, params) {
	return new Promise((resolve, reject) => {
		pool.getConnection(function (err, connection) {
			if (err) {
				reject(err);
				return;
			}

			connection.query(sql, params, function (err, results, fields) {
				console.log(`${sql} =>${params}`);

				// 释放连接
				connection.release();

				if (err) {
					reject(err);
					return;
				}

				resolve(results);
			});
		});
	});
};

module.exports = db;
